// 圆线宽度
var lineWidth = 0;
// 小球画布
var ballCanvas = null;

// 初始化所有小球.参赛选手可修改的函数,不可改变函数名, 不能改变函数的输入输出:无参数, 无返回值.
function initBallList(){
    ballList=[];

    for (var i=0; i<ballNum; i++){
        ballList.push(new Ball());
    }

    // 取得小圆线宽
    lineWidth = context.lineWidth;
    // 初始化ball canvas
    initBallCanvas();

    // 初始化背景canvas
    initBackgroundCanvas();
}

// 动画测试的核心函数.参赛选手可修改的函数,不可改变函数名, 不能改变函数的输入输出:参数deltaTime, 无返回值.
function testCore(deltaTime) {
    //canvas.width = 0;
    canvas.width = width;
    //context.clearRect(0, 0, width, height);

    for (var i=0; i < ballNum; i++) {
        var ball = ballList[i];
        ball.update(deltaTime);
        context.drawImage(ballCanvas, ball.x, ball.y);
    }
};

// 重构小球对象，使对象仅保存位置信息及相关运算操作，与canvas相关操作解耦
var Ball = function () {
        //小球初始位置
        this.x =  0,
        this.y = 0,
        // 初始化移动速度
        this.speedX = getNewSpeed();//
        this.speedY = getNewSpeed();
    };
//小球限定移动区域
Ball.minX = 0;
Ball.minY = 0;
Ball.maxX = width - 2 * ballRadius;
Ball.maxY = height - 2 * ballRadius;
// 检查X坐标
Ball.prototype.checkX = function () {
    var minX = Ball.minX;
    var maxX = Ball.maxX;
    if (this.x < minX){
        this.x = minX;
        this.speedX = getNewSpeed();//
        return;
    } else if (this.x > maxX){
        this.x = maxX;
        this.speedX = - getNewSpeed();//
        return;
    }
};
// 检查Y坐标
Ball.prototype.checkY = function () {
    var minY = Ball.minY;
    var maxY = Ball.maxY;
    if (this.y < minY){
        this.y = minY;
        this.speedY = getNewSpeed();
        return;
    } else if (this.y > maxY){
        this.y = maxY;
        this.speedY = - getNewSpeed();
        return;
    }
};
// 更新坐标数据
Ball.prototype.update = function (deltaTime) {
    this.x = this.x + this.speedX * deltaTime;
    this.y = this.y + this.speedY * deltaTime;
    this.checkX();
    this.checkY();
};

// 在内存中创建Canvas对象并返回
function createCanvas(width, height) {
    var canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    return canvas;
};

// 在内存中创建小球Canvas对象
function initBallCanvas() {
    ballCanvas = createCanvas(2 * (ballRadius + lineWidth), 2 * (ballRadius + lineWidth));
    var context = ballCanvas.getContext("2d");
    context.clearRect(0, 0, ballCanvas.width, ballCanvas.height);
    context.beginPath();
    context.arc(ballRadius + lineWidth, ballRadius + lineWidth, ballRadius, 0, 2 * Math.PI, false);
    context.fillStyle = ballColor;
    context.fill();
    context.stroke();
    context.closePath();
    context.drawImage(ImgCache["icon"], ballCanvas.width / 2 - iconRadius, ballCanvas.height / 2 - iconRadius);
};

// 在内存中创建背景Canvas对象,并转化为DataURL设为舞台背景，无需每次重绘背景
function initBackgroundCanvas() {
    var backgroundCanvas = createCanvas(canvas.width, canvas.height);
    var context = backgroundCanvas.getContext("2d");
    context.fillStyle = bgColor;
    context.fillRect(0, 0, canvas.width, canvas.height);
    context.drawImage(ImgCache["text"], 5, 50);
    context.drawImage(ImgCache["logo"], 225, 30);
    canvas.style.backgroundImage = "url("+ backgroundCanvas.toDataURL() +")";
}
